import 'package:cbec_app/components/base/base_image.dart';
import 'package:cbec_app/components/common/common_style.dart';
import 'package:cbec_app/components/goods/goods_item.dart';
import 'package:cbec_app/pages/goods/controller/recommend_ctr.dart';
import 'package:cbec_app/utils/Tools.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

///个性推荐组件
class Recommend extends StatefulWidget {
  final bool? isShowTitle;

  final String? location;

  final String? goodsId;

  const Recommend({super.key, this.location = '', this.goodsId = '', this.isShowTitle = true});

  @override
  State<Recommend> createState() => _RecommendState();
}

class _RecommendState extends State<Recommend> {
  late final RecommendController controller;

  @override
  void initState() {
    super.initState();
    controller = Get.put(RecommendController(), tag: widget.location);
    controller.getRecommendList(location: widget.location);
  }

  @override
  Widget build(BuildContext context) {
    return Obx(() => controller.data['status'] == true && controller.goodsList.isNotEmpty
        ? Container(
            child: Column(
              children: [
                SizedBox(
                  height: widget.isShowTitle == true ? 40 : 0,
                  child: Visibility(
                    visible: widget.isShowTitle ?? true,
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        BaseImage(url: "l_heart.png", width: 20, height: 20),
                        SizedBox(width: 5.w),
                        Text(controller.data['name'] == '个性推荐' ? '精选好物' : controller.data['name'], style: setTextStyle(15, FontWeight.w500, Tools.fromHex("#E74E50"))),
                        SizedBox(width: 5.w),
                        BaseImage(url: "r_heart.png", width: 20, height: 20),
                      ],
                    ),
                  ),
                ),
                Container(
                  // margin: EdgeInsets.only(left: 8.w, right: 8.w),
                  child: controller.goodsList.isNotEmpty
                      ? Wrap(
                          alignment: WrapAlignment.spaceBetween,
                          spacing: 6.w,
                          runSpacing: 10.w,
                          children: List.generate(controller.goodsList.length, (index) {
                            final Map<String, dynamic> item = controller.goodsList[index];
                            return GoodsItem(goodsInfo: item);
                          }))
                      : emptyWidget(),
                ),
                emptyMargin(ScreenUtil().bottomBarHeight + kTextTabBarHeight)
              ],
            ),
          )
        : emptyWidget());
  }
}
